package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.third.*;
import com.zmj.sy.mom.srv.aps.bean.vo.lantek.*;
import com.zmj.sy.mom.srv.aps.bean.vo.cuttingProgram.CuttingProgramResVo;
import com.zmj.sy.mom.srv.aps.config.ProjectConfig;
import com.zmj.sy.mom.srv.aps.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class CuttingProgramService {

    private final SinoCamReceiveNestNestMapper sinoCamReceiveNestNestMapper;
    private final SinoCamReceiveNestPlateMapper sinoCamReceiveNestPlateMapper;
    private final SinoCamReceiveNestPartMapper sinoCamReceiveNestPartMapper;
    private final SinoCamReceiveNestExcessMapper sinoCamReceiveNestExcessMapper;
    private final SinoCamReceiveNestMoveMapper sinoCamReceiveNestMoveMapper;
    private final ProjectConfig projectConfig;

    public CuttingProgramResVo getProgram(String plateCode) {
        CuttingProgramResVo vo = new CuttingProgramResVo();
        SinoCamReceiveNestPlate nestPlate = sinoCamReceiveNestPlateMapper.lambdaQuery().eq(SinoCamReceiveNestPlate::getMaterielName, plateCode).one();
        if (ObjectUtils.isEmpty(nestPlate)) {
            return null;
        }
        SinoCamReceiveNestNest nestNest = sinoCamReceiveNestNestMapper.lambdaQuery().eq(SinoCamReceiveNestNest::getReceiveNestId, nestPlate.getReceiveNestId()).one();
        if (ObjectUtils.isEmpty(nestNest)) {
            return null;
        }
        vo.setCuttingProgramUrl(nestNest.getCuttingProgramUrl());

        return vo;
    }

    public LantekReceiveNestReqVo getCutting(String plateCode) {

        // 新建回传对象
        LantekReceiveNestReqVo vo = new LantekReceiveNestReqVo();
        LantekReceiveNestNestReqVo nestVo = new LantekReceiveNestNestReqVo();
        List<LantekReceiveNestPlateReqVo> plateListVo = new ArrayList<>();
        List<LantekReceiveNestPartReqVo> partListVo = new ArrayList<>();
        List<LantekReceiveNestExcessReqVo> excessListVo = new ArrayList<>();
        List<LantekReceiveNestMoveReqVo> moveListVo = new ArrayList<>();

        // 获取到位钢板的 receive id
        SinoCamReceiveNestPlate plate = sinoCamReceiveNestPlateMapper.lambdaQuery().eq(SinoCamReceiveNestPlate::getStockListNo, plateCode).one();

        // 查询所有该 id 下的套料信息
        SinoCamReceiveNestNest nest = sinoCamReceiveNestNestMapper.lambdaQuery().eq(SinoCamReceiveNestNest::getReceiveNestId, plate.getReceiveNestId()).one();
        List<SinoCamReceiveNestPlate> plates = sinoCamReceiveNestPlateMapper.lambdaQuery().eq(SinoCamReceiveNestPlate::getReceiveNestId, plate.getReceiveNestId()).list();
        List<SinoCamReceiveNestPart> parts = sinoCamReceiveNestPartMapper.lambdaQuery().eq(SinoCamReceiveNestPart::getReceiveNestId, plate.getId()).list();
        List<SinoCamReceiveNestExcess> excesses = sinoCamReceiveNestExcessMapper.lambdaQuery().eq(SinoCamReceiveNestExcess::getReceiveNestId, plate.getId()).list();
        List<SinoCamReceiveNestMove> moves = sinoCamReceiveNestMoveMapper.lambdaQuery().eq(SinoCamReceiveNestMove::getReceiveNestId, plate.getId()).list();

        // 套料（切割）
        nestVo.setNestId(nest.getNestId());
        nestVo.setFactoryCode(nest.getFactoryIdk());
        nestVo.setCutLength(nest.getCutLength());
        nestVo.setCutNozzleNorm(nest.getCutNozzleNorm());
        nestVo.setCutNozzleNumber(nest.getCutNozzleNumber());
        nestVo.setCutNozzleDistance(nest.getCutNozzleDistance());
        nestVo.setWidth(nest.getWidth());
        nestVo.setLength(nest.getLength());
        nestVo.setNestDateTime(nest.getNestDate());
        nestVo.setNeedDateTime(nest.getNeedDate());
        nestVo.setNester(nest.getNester());
        nestVo.setOptionId(nest.getOptionId());
        nestVo.setGroupCode(nest.getSplitGroupCode());
        nestVo.setDxfUrl(nest.getNestDxfUrl());
        nestVo.setBookSheet(nest.getBookSheet());
        nestVo.setReportPdfUrl(nest.getReportPdfUrl());
        nestVo.setCuttingProgramUrl(nest.getCuttingProgramUrl());
        nestVo.setRate(nest.getRate());
        vo.setNest(nestVo);

        // 钢板
        for (SinoCamReceiveNestPlate nestPlate : plates) {
            LantekReceiveNestPlateReqVo plateVo = new LantekReceiveNestPlateReqVo();
            plateVo.setPlateCode(nestPlate.getStockListNo());
            plateVo.setFactoryCode(nestPlate.getFactoryIdk());
            plateVo.setTexture(nestPlate.getTexture());
            BigDecimal thickness = new BigDecimal(nestPlate.getNorm());
            plateVo.setThickness(thickness);
            plateVo.setWidth(nestPlate.getWidth());
            plateVo.setLength(nestPlate.getLength());
            plateVo.setStoreCode(nestPlate.getStorehouseId());
            plateVo.setSequence(nestPlate.getSequence());
            plateVo.setEmptyCutLength(nestPlate.getEmptyCutLength());
            plateVo.setCutTime(nestPlate.getCutTime());
            plateVo.setTotalCutLength(nestPlate.getTotalCutLength());
            plateVo.setRate(nestPlate.getRate());
            plateVo.setHoleCount(nestPlate.getHoleCount());
            plateVo.setSideLength(nestPlate.getSideLength());
            plateVo.setPngUrl(String.format(projectConfig.getUrlPre(), plateVo.getPlateCode() + ".png", nestPlate.getPngUrl()));
            plateVo.setPositionCode(nestPlate.getPositionCode());
            plateListVo.add(plateVo);
        }
        vo.setPlate(plateListVo);

        // 零件
        for (SinoCamReceiveNestPart nestPart : parts) {
            LantekReceiveNestPartReqVo partVo = new LantekReceiveNestPartReqVo();
            partVo.setPartPlanId(nestPart.getNestPlanId());
            partVo.setPartId(nestPart.getUniCode());
            partVo.setPartName(nestPart.getPartName());
            partVo.setQuantity(nestPart.getQuantity());
            partVo.setNetWeight(nestPart.getNetWeight());
            partVo.setArea(nestPart.getArea());
            partVo.setFactoryCode(nestPart.getRequireFactoryId());
            partVo.setWidth(nestPart.getWidth());
            partVo.setLength(nestPart.getLength());
            partVo.setThickness(nestPart.getThickness());
            partVo.setPngUrl(String.format(projectConfig.getUrlPre(), partVo.getPartId() + ".png", nestPart.getPartMapUrl()));
            partVo.setPlateCode(nestPart.getPlateCode());
            partListVo.add(partVo);
        }
        vo.setPart(partListVo);

        // 余料
        for (SinoCamReceiveNestExcess nestExcess : excesses) {
            LantekReceiveNestExcessReqVo excessVo = new LantekReceiveNestExcessReqVo();
            excessVo.setFactoryCode(nestExcess.getFactoryIdk());
            excessVo.setExcessCode(nestExcess.getStockListNo());
            excessVo.setPlateCode(nestExcess.getExMaterielId());
            excessVo.setTexture(nestExcess.getTexture());
            BigDecimal thickness = new BigDecimal(nestExcess.getNorm());
            excessVo.setThickness(thickness);
            excessVo.setWidth(nestExcess.getWidth());
            excessVo.setLength(nestExcess.getLength());
            excessVo.setWeight(nestExcess.getWeight());
            excessListVo.add(excessVo);
        }
        vo.setExcess(excessListVo);

        // 走刀
        for (SinoCamReceiveNestMove nestMove : moves) {
            LantekReceiveNestMoveReqVo moveVo = new LantekReceiveNestMoveReqVo();
            moveVo.setCutLength(nestMove.getCutLength());
            moveVo.setFactoryCode(nestMove.getFactoryIdk());
            moveVo.setMoveTime(nestMove.getMoveTime());
            moveVo.setMoveType(nestMove.getMoveType());
            moveVo.setHoleCount(nestMove.getHoleCount());
            moveVo.setHoleTime(nestMove.getHoleTime());
            moveVo.setTotalHoleTime(nestMove.getTotalHoleTime());
            moveVo.setCutRate(nestMove.getCutRate());
            moveVo.setTotalCutTime(nestMove.getTotalCutTime());
            moveListVo.add(moveVo);
        }
        vo.setMove(moveListVo);

        return vo;

    }

}
